package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Link;
import com.stu.boke.service.LinkService;

@Controller
@RequestMapping(value = "/admin/link")
public class LinkController {
	
	@Autowired
	private LinkService linkService;
	
	@RequestMapping("")
	public ModelAndView indexLink() {
		ModelAndView model = new ModelAndView();
		List<Link> linkList = linkService.listLink(null);
		model.addObject("linkList", linkList);
		model.setViewName("Admin/Link/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitLink(Link link) {
		linkService.insertSubmitLink(link);
		return "redirect:/admin/link";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editLinkView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Link linkCustom = linkService.getLinkById(id);
		modelAndView.addObject("linkCustom", linkCustom);
		modelAndView.setViewName("Admin/Link/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editLinkSubmit(Link link) {
		linkService.updateLink(link);
		return "redirect:/admin/link";
	}
	
	@RequestMapping(value = "/insert")
	public ModelAndView insertArticle(Model model) {
		ModelAndView modelAndView = new ModelAndView();
		/*
		 * List<Link> linkList = linkService.listLink();
		 * modelAndView.addObject("linkList", linkList);
		 */
        modelAndView.setViewName("Admin/Link/insert");
        return modelAndView;
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteLink(@PathVariable("id") Integer id) {
		linkService.deleteLink(id);
		return "redirect:/admin/link";
	}

}
